package com.docios.cookbook2.utils;

import android.content.Context;
import android.util.Log;

import com.docios.cookbook2.entity.Category;
import com.docios.cookbook2.entity.MaterialBean;
import com.docios.cookbook2.entity.ProcessBean;
import com.docios.cookbook2.entity.QueryBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class JsonUtil {
    /*
     * 一级嵌套为数组的解析
     * */
    public List<QueryBean> jsonForArray(String jsonString) throws JSONException {

        Gson gson = new Gson();
        JSONArray listArray = new JSONObject(jsonString).getJSONObject("result").getJSONArray("list");

        /*1层
         * 获取queryBean数组
         * queryBeans里没有material和process的数据！
         * */
        List<QueryBean> queryBeans = gson.fromJson(listArray.toString(), new TypeToken<List<QueryBean>>() {
        }.getType());

        /*2层
         * 获取 (material数组 和 process数组) 的数组
         * */
        for (int i = 0; i < listArray.length(); i++) {

            JSONObject listObject = listArray.getJSONObject(i);
            JSONArray materialArray = listObject.getJSONArray("material");
            JSONArray processArray = listObject.getJSONArray("process");

            List<MaterialBean> materialBeans = gson.fromJson(materialArray.toString(), new TypeToken<List<MaterialBean>>() {
            }.getType());
            List<ProcessBean> processBeans = gson.fromJson(processArray.toString(), new TypeToken<List<ProcessBean>>() {
            }.getType());

            //存入两数组
            queryBeans.get(i).setMaterialBeans(materialBeans);
            queryBeans.get(i).setProcessBeans(processBeans);
        }

        Log.i("mytag", "json: " + queryBeans.toString());

        return queryBeans;
    }

    /*
     * 一级嵌套为对象的解析
     * */
    public QueryBean jsonForObject(String jsonString) throws JSONException {

        Gson gson = new Gson();
        JSONObject listObject = new JSONObject(jsonString).getJSONObject("result");

        /*1层
         * 获取queryBean数组
         * queryBeans里没有material和process的数据！
         * */
        QueryBean queryBean = gson.fromJson(listObject.toString(), new TypeToken<QueryBean>() {
        }.getType());

        /*2层
         * 获取 (material数组 和 process数组) 的数组
         * */
        JSONArray materialArray = listObject.getJSONArray("material");
        JSONArray processArray = listObject.getJSONArray("process");

        List<MaterialBean> materialBeans = gson.fromJson(materialArray.toString(), new TypeToken<List<MaterialBean>>() {
        }.getType());
        List<ProcessBean> processBeans = gson.fromJson(processArray.toString(), new TypeToken<List<ProcessBean>>() {
        }.getType());

        //存入两数组
        queryBean.setMaterialBeans(materialBeans);
        queryBean.setProcessBeans(processBeans);

        Log.i("mytag", "json: " + queryBean);

        return queryBean;
    }

    /*
     * 本地分类id查询
     * */
    public ArrayList<QueryBean> localByClass(Context context) throws JSONException, IOException {

        // 获取json文件的输入流
        InputStream inputStream = context.getAssets().open("byType.json");
        int size = inputStream.available();
        byte[] buffer = new byte[size];
        inputStream.read(buffer);
        inputStream.close();
        //得到json字符串
        String jsonString = new String(buffer, StandardCharsets.UTF_8);


        Gson gson = new Gson();
        JSONArray listArray = new JSONObject(jsonString).getJSONObject("result").getJSONArray("list");

        /*1层
         * 获取queryBean数组
         * queryBeans里没有material和process的数据！    已解决
         * */
        ArrayList<QueryBean> queryBeans = gson.fromJson(listArray.toString(), new TypeToken<List<QueryBean>>() {
        }.getType());

        /*2层
         * 获取 (material数组 和 process数组) 的数组
         * */
        for (int i = 0; i < listArray.length(); i++) {

            JSONObject listObject = listArray.getJSONObject(i);
            JSONArray materialArray = listObject.getJSONArray("material");
            JSONArray processArray = listObject.getJSONArray("process");

            List<MaterialBean> materialBeans = gson.fromJson(materialArray.toString(), new TypeToken<List<MaterialBean>>() {
            }.getType());
            List<ProcessBean> processBeans = gson.fromJson(processArray.toString(), new TypeToken<List<ProcessBean>>() {
            }.getType());

            //存入两数组
            queryBeans.get(i).setMaterialBeans(materialBeans);
            queryBeans.get(i).setProcessBeans(processBeans);
        }

        Log.i("mytag", "json: " + queryBeans.toString());

        return queryBeans;
    }

    /*
     * 本地分类查询
     * */
    public List<Category> localClasses(Context context) throws JSONException, IOException {
        // 获取json文件的输入流 byClass
        InputStream inputStream = context.getAssets().open("type.json");
        int size = inputStream.available();
        byte[] buffer = new byte[size];
        inputStream.read(buffer);
        inputStream.close();
        //得到json字符串
        String jsonString = new String(buffer, StandardCharsets.UTF_8);


        Gson gson = new Gson();
        JSONArray resultArray = new JSONObject(jsonString).getJSONArray("result");
        /*1层
         * 获取一级分类：功效，人群，疾病。。。
         * */
        List<Category> categories = gson.fromJson(resultArray.toString(), new TypeToken<List<Category>>() {
        }.getType());

        /*2层
         * 获取 二级分类：减肥，美容，排毒。。。
         * */
        for (int i = 0; i < resultArray.length(); i++) {

            JSONObject CListObject = resultArray.getJSONObject(i);
            JSONArray CListArray = CListObject.getJSONArray("list");

            List<Category.CList> CLists = gson.fromJson(CListArray.toString(), new TypeToken<List<Category.CList>>() {
            }.getType());

            //存入数组
            categories.get(i).setCLists(CLists);
        }

        return categories;
    }

}